package activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.jaden.washer.ActivityManagement;
import com.example.jaden.washer.MyApplication;
import com.example.jaden.washer.R;
import com.example.jaden.washer.TransferData;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.MalformedURLException;
import java.util.List;

import customControls.DormitoryAdapter;
import dormitory.Dormitory;
import washer.Washer;

public class HomePage extends AppCompatActivity implements AdapterView.OnItemClickListener {

    MyApplication application;
    ListView dorm;
    List<Dormitory> dormitories;
    DormitoryAdapter dormitoryAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homepage);
        //将当前活动加入list中进行管理
        ActivityManagement.activityList.add(this);

        getInstance();
        initDorm();
        dorm.setOnItemClickListener(this);
    }
    //获取控件实例
    private void getInstance() {
        dorm=(ListView)findViewById(R.id.ListView_dormwashmachine_homepage);
        application=MyApplication.getInstance();

    }
    //初始化商品
    private void initDorm() {
        String url="http://47.96.159.236:8080/Wash/Dormitory?method=getDormitory";
        try {
            Gson gson=new Gson();
            //Dormitory dormitory=new Dormitory();
            //String message="{\"DormID\":\"\",\"DormLocation\":\"\", \"IsOpen\":, \"WasherNumber\":, \"FreeNumber\": }";
            String message="{ \"DormID\":\"\"}";
            TransferData.GetDormList(url,message);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        while(true) {
            if(application.isGetDormOK()) {
                application.setGetDormOK(false);
                dormitories= (List<Dormitory>) application.getResponseMessage().getData();
                dormitoryAdapter = new DormitoryAdapter(HomePage.this, R.layout.dorm_listview_item_washingmachine, dormitories);
                dorm.setAdapter(dormitoryAdapter);
                break;
            }
        }
    }

    /**
     *点击item进入某一宿舍 查看该宿舍的具体洗衣机使用情况
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Dormitory dormitory = dormitories.get(position);
        //如果该宿舍楼显示是营业中则获取该宿舍楼楼的洗衣机
        if (dormitory.getIsOpen()) {
            application.setCurDorm(dormitory);
            Intent intent = new Intent(this, WasherPage.class);
            startActivity(intent);
        }
        else
            Toast.makeText(HomePage.this,dormitory.getDormLocation()+"处于未营业状态",Toast.LENGTH_LONG).show();
    }
}
